/*
 * Copyright 2012 Antonio Leiva
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.undobar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ListView;

import com.example.android.undobar.UndoBarController.UndoListener;

public abstract class UndoableActivity extends Activity implements UndoListener {
	protected UndoBarController mUndoBarController;
	private boolean isMultiTouch;
	private float posY;
	private final float span = 66;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mUndoBarController = new UndoBarController(findViewById(R.id.undobar),
				this);
	}


	protected void makeListViewUndoable(ListView view) {
		
		view.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				int action = event.getAction() & MotionEvent.ACTION_MASK;

				switch (action) {
				case MotionEvent.ACTION_POINTER_DOWN: {
					isMultiTouch = true;

					posY = event.getAxisValue(MotionEvent.AXIS_Y);
					break;
				}
				case MotionEvent.ACTION_POINTER_UP: {
					isMultiTouch = false;
					break;
				}
				case MotionEvent.ACTION_MOVE: {
					if (isMultiTouch) {
						float posAct = event.getAxisValue(MotionEvent.AXIS_Y);

						if (Math.abs(posAct - posY) > span && posAct < posY) {
							
							mUndoBarController.showUndoBar(false,
									getString(R.string.undobar_sample_message),
									null);
						}
					}

					break;
				}
				}

				if (isMultiTouch) {
					return true;
				} else {
					return v.onTouchEvent(event);
				}
			}
		});
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);

		int action = event.getAction() & MotionEvent.ACTION_MASK;

		switch (action) {		
		case MotionEvent.ACTION_POINTER_DOWN: {
			isMultiTouch = true;

			posY = event.getAxisValue(MotionEvent.AXIS_Y);

			break;
		}
		case MotionEvent.ACTION_POINTER_UP: {
			isMultiTouch = false;

			break;
		}
		case MotionEvent.ACTION_MOVE: {
			if (isMultiTouch) {
				float posAct = event.getAxisValue(MotionEvent.AXIS_Y);

				if (Math.abs(posAct - posY) > span && posAct < posY) {
					
					mUndoBarController.showUndoBar(false,
							getString(R.string.undobar_sample_message), null);
				}
			}

			break;
		}
		}

		return true;
	}

	@Override
	public abstract void onUndo(Parcelable token);
}
